package com.lzxxkjxx.bigwork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {



    private Context mContext;
    private EditText username,password;
    private final String TAG = "MainActivity";
    private CheckBox chk;
    private Button btn_login;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;
        Button button_login = findViewById(R.id.bt_2);
        CheckBox checkBox_MemoryUserPassword = findViewById(R.id.MemoryPassword);
        editor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
        TextView textView1 = findViewById(R.id.text_1);
//        Button buttonservice1 = findViewById(R.id.service1);
//        Button buttonservice2 = findViewById(R.id.service2);
        CheckUserPasswordMemory();//调用函数

        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = ((EditText)findViewById(R.id.Ed_1)).getText().toString().trim();
                String password = ((EditText)findViewById(R.id.Ed_2)).getText().toString().trim();
                if(username.isEmpty() && password.isEmpty()){
                    Toast.makeText(getApplicationContext(),"用户名和密码不能为空",Toast.LENGTH_LONG).show();
                }else {
                    if ((username.equals("admin"))&&(password.equals("12345678"))){
                        if(checkBox_MemoryUserPassword.isChecked()){
                            editor.putString("username",username);
                            editor.putString("password",password);
                            editor.putBoolean("memoryPassword",true);
                        }else {
                            editor.clear();
                        }
                        editor.apply();
                        Intent intent = new Intent(MainActivity.this,MemorandumActivity.class);//登录成功后调入备忘录
                        intent.putExtra("text1",username);
                        startActivity(intent);
                        Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(getApplicationContext(),"用户名不存在",Toast.LENGTH_LONG).show();
                    }
                }
            }
        });



        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);//注册
                startActivity(intent);

            }
        });




    }

    private void CheckUserPasswordMemory() {
        EditText Ed_username = findViewById(R.id.Ed_1);
        EditText Ed_password = findViewById(R.id.Ed_2);
        CheckBox checkBox_memoryUserPassword = findViewById(R.id.MemoryPassword);
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        if (pref.getBoolean("memoryPassword", false) == true) {
            String data_username = pref.getString("username", "");
            String data_password = pref.getString("password", "");
            Ed_username.setText(data_username);
            Ed_password.setText(data_password);
            checkBox_memoryUserPassword.setChecked(true);
        }
    }

    long time;
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if(System.currentTimeMillis()-time>2000){
                Toast.makeText(MainActivity.this,"再按一次退出哟！",Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
            }else{
                    finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }


}